﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BaccaratRally.DataKernel.Service
{
    public class ServiceFactory
    {
        protected static Dictionary<Type, Type> ServicePool
        {
            get;
            private set;
        }

        private static object _syncLock = new object();

        static ServiceFactory()
        {
            ServicePool = new Dictionary<Type, Type>();
        }

        public static T CreateService<T>() where T : ServiceBase
        {
#if DEBUG
            return Activator.CreateInstance<T>();
#else
            Type sevType = typeof(T);

            if (!ServicePool.ContainsKey(sevType))
            {
                lock (_syncLock)
                {
                    if (!ServicePool.ContainsKey(sevType))
                    {
                        Type proxyType = Wrapper.CreateWrapper<T>();
                        ServicePool.Add(sevType, proxyType);
                    }
                }
            }

            T returnObject = (T)Activator.CreateInstance(ServicePool[sevType]);

            return returnObject;
#endif
        }
    }
}
